﻿namespace Filters.Demo.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class ImageCatalogRepository : IImageCatalogRepository
    {
        public IList<ImageModel> GetImageModels()
        {
            using (var entities = new ImageCatalogEntities())
            {
                return entities.Signatures
                    .ToList()
                    .ConvertAll(s => s.ToModel());
            }
        }

        public IList<ImageModel> GetCluster(int pivotId)
        {
            using (var entities = new ImageCatalogEntities())
            {
                return entities.Pivots.Include("SignaturesCluster")
                    .First(p => p.PivotID == pivotId)
                    .SignaturesCluster
                    .ToList()
                    .ConvertAll(s => s.ToModel());
            }
        }

        public IList<PivotModel> GetPivots()
        {
            using (var entities = new ImageCatalogEntities())
            {
                return entities.Pivots.Include("PivotSignature")
                    .ToList()
                    .ConvertAll(p => p.ToModel());
            }
        }

        public void CreateImageModel(ImageModel imageModel)
        {
            using (var entities = new ImageCatalogEntities())
            {
                entities.AddToSignatures(imageModel.ToData(entities.Pivots));
                entities.SaveChanges();
            }
        }

        public void CreatePivots(IEnumerable<PivotModel> pivotModels)
        {
            using (var entities = new ImageCatalogEntities())
            {
                foreach (var pivotModel in pivotModels)
                {
                    var pivotData = pivotModel.ToData(entities.Signatures);

                    entities.AddToPivots(pivotData);
                }

                entities.SaveChanges();

                foreach (var pivotData in entities.Pivots)
                {
                    var imageId = pivotData.PivotSignature.ImageID;

                    pivotModels.First(pm => pm.ImageModel.ImageID == imageId).PivotID = pivotData.PivotID;
                }
            }
        }

        public void UpdatePivotInformation(IList<ImageModel> imageModels)
        {
            using (var entities = new ImageCatalogEntities())
            {
                foreach (var imageModel in imageModels)
                {
                    var imageId = imageModel.ImageID;
                    var pivotId = imageModel.PivotID;
                    var imageEntity = entities.Signatures.First(sg => sg.ImageID == imageId);

                    imageEntity.DistanceToPivot = imageModel.DistanceToPivot;
                    imageEntity.Pivot = entities.Pivots.First(pv => pv.PivotID == pivotId);
                }

                entities.SaveChanges();
            }
        }

        public void ClearPivots()
        {
            using (var entities = new ImageCatalogEntities())
            {
                foreach (var pivot in entities.Pivots)
                {
                    entities.DeleteObject(pivot);
                }

                entities.SaveChanges();
            }
        }
    }
}
